Digby’s Real Estate 1.0.0

Dynamic Memory Allocation


If there is a conflict between the subject and the skeleton or moulinette, the subject is always right.
However, please report any inconsistencies to your assistant.

Submissions

Repository structure
At the end, your git repo must follow this architecture:
prog-102-p-03-2030-firstname.lastname
├── Digby_Real_Estate
│   ├── Fundamentals
│   │   ├── count.c
│   │   ├── most_frequent.c
│   │   ├── my_factor.c
│   │   ├── my_strapp.c
│   │   ├── my_weirddup.c
│   │   ├── replace.c
│   │   └── trap_sweeper.c
│   └── Proficiencies
│       ├── better_replace.c
│       ├── nearest_target.c
│       └── split_words.c
├── .gitignore
└── README

Do not forget to check the following requirements before submitting your work:
  • You shall obviously replace firstname.lastname with your login.
  • The .gitignore file is mandatory.
  • Remove all personal tests from your code, except those from the Tests folder.
  • The given prototypes must be strictly respected.
  • The code MUST compile! Otherwise you will not receive a grade.

.gitignore example
Here is an example of a .gitignore file:
*.a
*.lib
*.o
*.obj
*.out

.idea/
*~
*.DotSettings.user

This needs to be setup before the first submission!

Introduction


Information
In this practical, you will use malloc(3) and all functions associated a lot.
You can find all these functions in the <stdlib.h> library, we encourage you to look at their man page using the man 3 malloc command.
Moreover, remember that everything that is malloc-allocated will need to be free at some point, sometimes you will do it, some times it will be us. Just remember to always ask yourself "Do I need to free this variable now or in another function ?" and to always code as if a psychopath were behind you.

Warning(s)
For this practical, the use of static arrays is prohibited.




Fundamentals/count.c


In this exercise, you'll need to implement the function count which takes an int n, your goal is to allocate an array that fits the integers from 1 to n and return it.
If malloc fails, you must return NULL.

Information
If n is less than 1, you must return NULL.

Prototype(s)
int *count(int n);

Output
count(0);  // NULL
count(1);  // {1}
count(6);  // {1, 2, 3, 4, 5, 6}
count(7);  // {1, 2, 3, 4, 5, 6, 7}



Fundamentals/my_weirddup.c


In this exercise, you'll need to implement the function my_weirddup which takes the string src, frees it and returns a copy of that string except that, for each letter of the string, you'll have to replace it with the next letter. Don't forget to come back to the first letter when you need to duplicate a 'z'.
If malloc fails, you must return NULL.

Information
if src is NULL you must return NULL.

Warning(s)
Do not forget to malloc enough space to put the null terminator !

Prototype(s)
char *my_weirddup(char *src);

Output
my_weirddup("a");        // "b"
my_weirddup("abcxyz");   // "bcdyza"
my_weirddup("ABC 123");  // "BCD 123"

Now, you'll have to write a program that takes all given arguments and prints the following output : <default string> ; <duplicate string>\n, using the function you just created.
This program must return 0 everytime.

Information
You’ll need to use strdup(3) to pass the arguments into your functions as arguments aren’t malloc-allocated.

Hint(s)
strdup(3) is a functions of the <string.h> library. This function returns a malloc-allocated copy of the string passed as an argument to the function.
Beware that, you will need the returned string to be freed if you don't want to have memory leaks.
Moreover, we HIGHLY advise you to look at the man page (with the man 3 strdup command).

Output
$ ./my_weirddup a abcxyz ABC123
a ; b
abcxyz ; bcdyza
ABC123 ; BCD123



Fundamentals/my_strapp.c


Here, you will learn how tu use the realloc function.
The my_strapp function takes two arguments, the src which is the string that we want to append and the dest pointer which points to the string where we want to append.
You may ask yourself why we use double-pointers.
The answer is that, it is to keep track of the string : realloc may change the base address of the pointer. To prevent any error, we stock the base string in another pointer.
Let's get back to the exercise, the function does nothing if src is NULL or dest is NULL. If the string pointed by dest is NULL, you can create the string with your own hands.

Prototype(s)
void my_strapp(char *src, char **dest);

Output
char *src = strdup("World!");
char *dest = strdup("Hello, ");
char **dest_ptr = &dest;
my_strapp(src, dest_ptr); // Hello, World!
free(src);
free(dest);



Fundamentals/my_factor.c


Let’s make a first malloc, just to see how it works.
The goal of this exercise is to write the function my_factor that returns an array containing the factors of the number n, excluding 1 and n itself.
If n is negative or equal to 0, you must return NULL.
Finally, you remember about strings finishing with the '\0' character ? We will do the same, be sure that your array finishes with the number 0.
If malloc fails, you must return NULL.

Information
If you want examples on what the function should return, we advise you to scroll to the real program examples.

Prototype(s)
int *my_factor(int n);

Now that you made the "main" function, you'll have to do the actual main function. You know, the one that is used by the program.
The goal of the program is to print the factors of each given arguments. It must always return 0.
You must follow the given pattern :

Output
Factors :
[1st argument] : [its factors separated by spaces]
[2nd argument] : [its factors separated by spaces aswell]
.
.
.

Hint(s)
Note that, if there are no arguments, you must do nothing.
You will only be tested on numbers, so don’t worry about getting anything else in the arguments

Information
Don't forget to use int argc and char *argv[] as arguments for your main function as so :
int main(int argc, char *argv[])

with argc as the number of arguments, and argv the array containing the strings of each argument.

Information
You can use the atoi(3) function to help you in this exercise. The manual is accessible with man 3 atoi.

Prototype(s)
int main (int argc, char *argv[]);

Output
$ ./factors 10 42069 -1 0
Factors :
10 : 2 5
42069 : 3 37 111 379 1137 14023
-1 : NULL
0 : NULL
$ echo $? # This prints the return code of last command in the terminal
0
$ ./factors
$ echo $?
0

Warning(s)
Do not forget to free everything that you allocated with malloc, realloc or calloc.



Fundamentals/replace.c


The function that you will write will take two arguments: a string str and another string sub. The goal of the function is to replace all asterisks (*) in the string str by the string sub. You must then return a new pointer to the result string.
If str is NULL or if the allocation fails, you must return NULL. But if sub is NULL, it is like working with "" (you should replace the asterisks by an empty string).

Prototype(s)
char *replace(char *str, char *sub);

Output
replace("Hello *", "World!");     // Hello World!
replace("*H*e*l*l*o*", "BIM");    // BIMHBIMeBIMlBIMlBIMoBIM



Fundamentals/most_frequent.c


The goal of this exercise is to write the function most_frequent that takes in parameters a single string and returns the most frequent character of that string.
In case of a tie, it will return the first one encountered.

Information
- If the pointer is NULL, return \0.
- If malloc fails, you must return \0.
- It can be any ascii character in any order

Prototype(s)
char most_frequent(const char *str);

Output
most_frequent("WRYYYY!1!");       // 'Y'
most_frequent("ORA ORA ORA!");    // 'O'
most_frequent("hello warudo!");   // 'l'
most_frequent("oRA ora oRa?");    // 'o'

Warning(s)
Remember that you can’t use static arrays, so we advise you to create your own array using malloc or calloc.



Fundamentals/trap_sweeper.c


The goal of this exercise is to create the function trap_sweeper that will take the matrix garden as argument and will return another matrix.
Each cell of the new matrix will contain the number of traps in the adjacent cells (vertically, horizontally and diagonally) in the old matrix.
Traps are determined by the '#' symbol.
Cells that are trapped will become -1 in the final matrix.
If any of the arguments is invalid, return NULL.
If you happen to not find any target nearby, you must set the cell to 0.
If any malloc fails, you must free everything (excluding the arguments) and return NULL.

Hint(s)
Remember that to allocate a matrix you must allocate the number of rows of your matrix times the size of a pointer, in our case, an int * (even though all data pointers share the same size).

Prototype(s)
int **trap_sweeper(char **mat, int rows, int cols);

Code example(s)
#include <stdio.h>
int main()
{
    char *mat1[] = {
        "OOO#O",    // 1 1 2 -1 2
        "#OO#O",    // -1 3 4 -1 2
        "O##OO",    // 2 -1 -1 3 2
        "OOOO#"     // 1 2 2 2 -1
    };
    int **res = trap_sweeper(mat1, 4, 5);
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 5; ++j)
            printf("%d ", res[i][j]);
        printf("\n");
    }

    for (int i = 0; i < 4; ++i)
        free(res[i]);
    free(res);
}

Output
1 1 2 -1 2
-1 3 4 -1 2
2 -1 -1 3 2
1 2 2 2 -1





Proficiencies/split_words.c


The goal of this exercise is to write the function split_words that will take as argument a string and will return an array of strings containing the different words of the first one, this array must be NULL terminated.
A word is considered a word when it is separated by at least one space.
If any malloc fails or if you don't find any words, you must free everything (excluding the arguments) and return NULL.

Information
We advise you to take a look at the isspace(3) function.

Prototype(s)
char **split_words(char *str);

Output
split_words("Hello World!"); // "Hello", "World!", NULL
split_words("Hello World! I am here !"); // "Hello", "World!", "I", "am", "here", "!", NULL



Proficiencies/better_replace.c


This function is very similar to the replace function, except that instead of replacing each asterisk, you will take a third argument (the to_replace string) and replace every occurence of this string in the str string by the sub string.
If str is NULL or if malloc fails, you must return NULL.
If to_replace is NULL, you just return a duplicate of str.
And if sub is NULL, it is the same thing as it being an empty string.

Warning(s)
Beware, the function is case-sensitive. Which means that ‘A’ and ‘a’ are not considered the same letter.

Prototype(s)
char *better_replace(char *str, char *sub, char *to_replace);

Output
better_replace("Hello You!", "World", "You");              // Hello World!
better_replace("The name of my dog is dog", "cat", "dog"); // The name of my cat is cat



Proficiencies/nearest_target.c


The goal of the exercise is to create the nearest_target function that takes a matrix of characters and a character target. Your function will return a matrix of the same size as the one taken as a parameter but instead of the characters we expect you to put a int that will represent the distance between that cell and the cell containing the nearest target.
If any of the arguments is invalid, return NULL.
If any malloc fails, you must free everything (excluding the arguments) and return NULL.

Information
If there are no targets, you must fill all the cells with -1.

Warning(s)
Do note that the distance between two adjacent cells on a diagonal will be two.
In short, the distance between a cell and a target is the amount of vertical and horizontal moves you need to reach the target from the cell.

Information
We highly advise you to create a matrix to track visited spaces and to create an auxiliary function

Prototype(s)
int **nearest_target(char **mat, int rows, int cols, char target);

Output
char *mat1[] = {
    "ab",   
    "aa"     
};

char *mat2[] = {
    "aba",    
    "aaa",  
    "baa"   
};

int **res1 = nearest_target(mat1, 2, 2, 'b');
int **res2 = nearest_target(mat2, 3, 3, 'b');

// res1 = {
//  { 1,0 },
//  { 2,1 }
// }

// res2 = {
//  { 1,0,1 },
//  { 1,1,2 },
//  { 0,1,2 }
// }


This page and all subpages are for internal use at EPITA only.
The use of this document must abide by the following rules:
Copyright © 2026-2027 - EPITA